/*
 * $Id: Opened.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.PlatformObject;

/**
  * ClientUser object for the 'p4 opened' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Opened extends ClientUser implements IPerforceCommand
{
	private List result = new ArrayList();
	private IStatus status;
	
	/**
	  * Convenience method for creating instances of Opened.
	  */
	public static final Opened newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Opened(status);
	}
 
	/**
	  * Creates a new instance.
	  */
	private Opened(MultiStatus status)
	{		
		super(status);
		this.status = status;
	}

	/** @see IClientUser#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		Result tempResult = new Result();
		tempResult.depotFilename = Helper.safeGetString(strDict, "depotFile"); //$NON-NLS-1$
		tempResult.clientFilename = Helper.safeGetString(strDict, "clientFile"); //$NON-NLS-1$
		tempResult.revision = Helper.safeGetInteger(strDict, "rev"); //$NON-NLS-1$
		tempResult.action = Helper.safeGetString(strDict, "action"); //$NON-NLS-1$
		tempResult.changelist = Helper.safeGetChangelist(strDict, "change"); //$NON-NLS-1$
		tempResult.type = Helper.safeGetString(strDict, "type"); //$NON-NLS-1$
		tempResult.userName = Helper.safeGetString(strDict, "user"); //$NON-NLS-1$
		tempResult.clientName = Helper.safeGetString(strDict, "client"); //$NON-NLS-1$
		
		result.add(tempResult);
	}
	
	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "opened"; //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public IOpenedResult[] getResult()
	{
		return (IOpenedResult[])result.toArray(new IOpenedResult[result.size()]);
	}	

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return status;
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	
	
	/**
	  * Implementation of the IOpenedResult. Stores information provided
	  * by the 'p4 opened' command.
	  * 
	  * @see IOpenedResult
	  */	
	private static class Result extends PlatformObject implements IOpenedResult
	{
		private String action;
		private Integer changelist;
		private String clientName;
		private String clientFilename;
		private String depotFilename;
		private Integer revision;
		private String type;
		private String userName;
		
		/** @see IOpenedResult#getAction() */
		public String getAction() { return action; }

		/** @see IOpenedResult#getChangelist() */
		public Integer getChangelist() { return changelist; }

		/** @see IOpenedResult#getClientName() */
		public String getClientName() { return clientName; }

		/** @see IOpenedResult#getClientSyntax() */
		public String getClientFilename() { return clientFilename; }

		/** @see IOpenedResult#getDepotFilename() */
		public String getDepotFilename() { return depotFilename; }

		/** @see IOpenedResult#getRevision() */
		public Integer getRevision() { return revision; }

		/** @see IOpenedResult#getType() */
		public String getType() { return type; }

		/** @see IOpenedResult#getUserName() */
		public String getUserName() { return userName; }
	}		
}
